<template>
  <div class="bg-white rounded-lg p-5 h-full">
    <div class="font-bold text-lg mb-4">预防接种机构管理</div>

    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-row :gutter="15">
        <el-form-item prop="enterpriseName">
          <el-input
            v-model="queryParams.enterpriseName"
            placeholder="医疗机构名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="type">
         
          <el-select
            v-model="queryParams.town"
            
            filterable
            remote
            clearable
            reserve-keyword
            placeholder="输入街道名称搜索"
            :remote-method="remoteMethod"
           
          >
            <el-option
              v-for="item in streetOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item prop="type">
          <el-select
            v-model="queryParams.typeIdList"
           
            placeholder="请选择机构类别"
           
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.enterprise_info_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item prop="type">
          <el-select
            v-model="queryParams.typeIdList"
          
            placeholder="请选择医疗机构等级"
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.medical_level"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-row>
      <el-row>
        <el-button type="primary"  @click="search"
          >搜索</el-button
        >
        <el-button @click="reset">重置</el-button>
      </el-row>
    </el-form>

    <el-table
      class="mt-4"
      v-loading="loading"
      :data="dataList"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="index" width="55" align="center" label="序号" />
      <el-table-column
        label="医疗机构名称"
        align="center"
        prop="enterpriseName"
      />
      <el-table-column
        label="机构类别"
        align="center"
        prop="categoryListName"
      />
      <el-table-column label="所在区/县" align="center" prop="county" />
      <el-table-column label="镇/街道" align="center" prop="town" />
      <el-table-column label="医院级别" align="center" prop="medicalLevel">
        <template #default="scope">
          <dict-tag
            :options="dict.type.medical_level"
            :value="scope.row.medicalLevel"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="是否预防接种机构"
        align="center"
        prop="preventStatus"
      >
        <template #default="scope">
          <dict-tag
            :options="dict.type.yes_or_no"
            :value="scope.row.preventStatus"
          />
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150px"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toStaffPage(scope.row)"
            >查看人员</el-button
          >
          <el-button size="mini" type="text" @click="toVaccinesPage(scope.row)"
            >疫苗管理</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getDataList"
    />
  </div>
</template>

<script>
import { getPreventListAPI } from "@/api/zhangping/health-care/prevent-vaccinate";
import { categoryList } from "@/api/zhangping/category";
import { getZpStreetListApi } from "@/api/common";
export default {
  dicts: [
    "enterprise_info_type",
    "health_certificate_status",
    "business_file_status",
    "enterprise_category",
    "yes_or_no",
    "medical_level",
  ],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      streetOptions:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 一企一档表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        enterpriseName: null,
        creditCode: null,
        type: null,
        typeIdList: [],
        categoryIdList: [],
        category: null,
        town: null,
       
        addr: null,
        legalPerson: null,
        contactNumber: null,
        businessLicense: null,
        sanitaryPermit: null,
        sanitaryPermitDate: null,
        healthCertificateStatus: null,
        archivesStatus: null,
        lastUpdateTime: null,
        userId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      detailForm: {},
      isEdit: false,
      editVisible: false,
      categoryList: [],
    };
  },
  created() {
    // this.loading = false;
    this.getDataList();
    this.remoteMethod()
    // this.getCategoryList()
  },
  methods: {
    remoteMethod(query){
      // const town = this.queryParams.town;
      // console.log(town);
      
      const params = {
        mname: query,
      }
      console.log(params);
      
      getZpStreetListApi(params).then((response) => {
        if (response.code === 200) {
          this.streetOptions = response.data.map(item=>{
            return {
              value: item.mname,
              label: item.mname,
            }
          }); 
        }
        
      });
    },
    toStaffPage(data) {
      const obj = {
        path: "/health-care/prevent-vaccines/staff",
        params: {
          creditCode: data.creditCode,
        },
      };
      this.$router.push(obj);
    },
    toVaccinesPage(data) {
      const obj = {
        path: "/health-care/prevent-vaccines/vaccines",
        params: {
          creditCode: data.creditCode,
        },
      };
      this.$router.push(obj);
    },
    /** 查询医疗废弃物列表 */
    getDataList() {
      this.loading = true;
      getPreventListAPI(this.queryParams)
        .then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.$refs["queryForm"].resetFields();
      this.search();
    },
    /** 搜索按钮操作 */
    search() {
      this.queryParams.pageNum = 1;
      this.getDataList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.search();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.detailForm = {};
      this.isEdit = false;
      this.editVisible = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      // this.reset();
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.detailForm = response.data;
        if (type == 1) {
          this.isEdit = true;
        } else {
          // 查看
          this.isEdit = false;
        }
        this.editVisible = true;
      });
    },
    /** 提交按钮 */
    submitForm(form, id) {
      if (id != null) {
        updateInfo(form).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.editVisible = false;
          this.getDataList();
        });
      } else {
        addInfo(form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.editVisible = false;
          this.getDataList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除一企一档编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getDataList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "enterpriseInfo/info/export",
        {
          ...this.queryParams,
        },
        `info_${new Date().getTime()}.xlsx`
      );
    },
    getCategoryList() {
      categoryList().then((response) => {
        this.categoryList = response.data;
      });
    },
  },
};
</script>
